package com.fs.system.teacher.service;

import cn.hutool.core.util.ObjectUtil;
import com.fs.system.college_admin.mapper.ActiveCreditMapper;
import com.fs.system.college_admin.mapper.ActiveTypeMapper;
import com.fs.system.exception.CustomerException;
import com.fs.system.manager.domain.Account;
import com.fs.system.student.domain.ApplicationRecord;
import com.fs.system.student.mapper.StudentMapper;
import com.fs.system.teacher.domain.First;
import com.fs.system.teacher.domain.Teacher;
import com.fs.system.teacher.mapper.TeacherMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;
import java.util.List;
import java.util.Objects;

@Service
public class TeacherService {

    @Autowired
    TeacherMapper teacherMapper;

    @Autowired
    StudentMapper studentMapper;

    @Autowired
    ActiveTypeMapper activeTypeMapper;

    @Autowired
    ActiveCreditMapper activeCreditMapper;
    /**
     * 教师修改个人信息
     * @param teacher
     */
    public void update(Teacher teacher) {
        //首先查询是否有该id的数据
        Teacher dbteacher =  teacherMapper.selectById(teacher.getTeaId());
        if(Objects.isNull(dbteacher)){
            throw new CustomerException("500","更新不合法");
        }
        teacher.setUpdateTime(LocalDateTime.now());
        teacherMapper.updateTeacher(teacher);
    }

    /**
     * 院长管理员添加单个教师信息
     * 默认字段：password，role，status，isDeleted
     */
    public void addOneTeacher(Teacher teacher) {
        //用户名唯一，首先查询是否存在该用户名
        Teacher dbteacher =  teacherMapper.selectByUsername(teacher.getUsername());
        if(!Objects.isNull(dbteacher)){
            throw new CustomerException("500","添加不合法");
        }
        //添加上默认字段
        teacher.setPassword("123456");
        teacher.setRole("TEACHER");
        teacher.setDeleted(false);
        teacher.setStatus("0");
        teacher.setCreateTime(LocalDateTime.now());
        teacherMapper.insertOne(teacher);
    }

    /**
     * 管理员修改单个教师信息，首先查询是否存在该教师
     * @param teacher
     */
    public void modifyTeacher(Teacher teacher) {
        Teacher dbteacher =  teacherMapper.selectById(teacher.getTeaId());
        if(Objects.isNull(dbteacher)){
            throw new CustomerException("500","更新不合法");
        }
        teacher.setUpdateTime(LocalDateTime.now());
        teacherMapper.updateTeacherByAdmin(teacher);
    }

    /**
     * 院管理员删除单个教师信息
     * @param teaId
     */
    public void deleteTeacher(Long teaId,String updateBy) {
        Teacher dbteacher =  teacherMapper.selectById(teaId);
        if(Objects.isNull(dbteacher)){
            throw new CustomerException("500","删除不合法");
        }
        teacherMapper.deleteTeacher(teaId,updateBy,LocalDateTime.now());
    }

    /**
     * 院管理员查询
     *
     * @param teacher
     * @return
     */
    public PageInfo<Teacher> selectTeacher(Teacher teacher , Integer page, Integer size) {
        PageHelper.startPage(page,size);
        List<Teacher> list = teacherMapper.selectAll(teacher);
        return PageInfo.of(list);
    }

    /**
     * 查询所有教师，支持模糊查询
     *
     * @param teacher
     * @return
     */
    public List<Teacher> selectAllTeacher(Teacher teacher) {
        return teacherMapper.selectAll(teacher);
    }

    /**
     * 教师管理员登录
     * @param account
     * @return
     */
    public Map<String, Object> login(Account account) {
        Map<String, Object> resMap = new HashMap<>();
        Teacher dbTeacher = teacherMapper.selectByUsername(account.getUsername());
        if(Objects.isNull(dbTeacher)){
            throw new CustomerException("500","用户名不存在");
        }
        if(!"0".equals(dbTeacher.getStatus())){
            throw new CustomerException("500","用户状态不合法");
        }
        //用户存在
        //判断密码是否正确
        if(!dbTeacher.getPassword().equals(account.getPassword())){
            throw new CustomerException("500","用户名或密码错误");
        }
        //所有逻辑判断没有问题，返回前端所需
        resMap.put("teaId", dbTeacher.getTeaId());
        resMap.put("username", dbTeacher.getUsername());
        resMap.put("realname", dbTeacher.getRealname());
        resMap.put("collegeId", dbTeacher.getCollegeId());
        resMap.put("status", dbTeacher.getStatus());
        resMap.put("role", dbTeacher.getRole());
        return resMap;
    }

    /**
     * 修改教师管理员密码
     * @param account
     */
    public void updatePassword(Account account) {
        /*从数据库拿出对象*/
        Teacher dbTeacher = teacherMapper.selectByUsername(account.getUsername());
        if (ObjectUtil.isNull(dbTeacher)) {
            throw new CustomerException("500","用户不存在");
        }
        /*判断原本输入传过来的原密码和查到的用户在数据库的密码是否一致*/
        if (!account.getPassword().equals(dbTeacher.getPassword())) {
            throw new CustomerException("500","原密码错误");
        }
        dbTeacher.setPassword(account.getNewPassword());
        dbTeacher.setUpdateBy(account.getUsername());
        dbTeacher.setUpdateTime(LocalDateTime.now());
        /*将更新过的对象再更新到数据库里面*/
        teacherMapper.updateTeacher(dbTeacher);
    }

    public Teacher selectById(Teacher teacher) {
        return teacherMapper.selectById(teacher.getTeaId());
    }

    /**
     * 教师获取待初审的信息
     * 根据老师id和状态查询
     * 待初审的status为2
     * @return
     */
    public PageInfo<ApplicationRecord> getFirst(ApplicationRecord applicationRecord,Integer page, Integer size) {
        applicationRecord.setStatus("2");
        PageHelper.startPage(page,size);
        List<ApplicationRecord> list = teacherMapper.selectFirst(applicationRecord);
        //根据id查询出活动类型，名称，学生姓名
        for(ApplicationRecord a:list){
            a.setStuName(studentMapper.selectStudentById(a.getStuId()).getRealname());
            a.setTypeName(activeTypeMapper.selectActiveTypeById(a.getTypeId()).getName());
            a.setActName(activeCreditMapper.selectActiveCreditById(a.getActId()).getActName());
        }
        return PageInfo.of(list);
    }

    /**
     * 初审材料，修改status为3
     * @param applicationRecord
     */
    public void firstCheck(ApplicationRecord applicationRecord) {
        //applicationRecord.setStatus("3");
        teacherMapper.firstUpdate(applicationRecord);

    }

    /**
     * 教师查询7天前待初审的的数据
     * @param applicationRecord
     * @return
     */
    public PageInfo<ApplicationRecord> getFirst2(ApplicationRecord applicationRecord,Integer page, Integer size) {
        applicationRecord.setStatus("2");
        PageHelper.startPage(page,size);
        List<ApplicationRecord> list = teacherMapper.selectFirst2(applicationRecord);
        //根据id查询出活动类型，名称，学生姓名
        for(ApplicationRecord a:list){
            a.setStuName(studentMapper.selectStudentById(a.getStuId()).getRealname());
            a.setTypeName(activeTypeMapper.selectActiveTypeById(a.getTypeId()).getName());
            a.setActName(activeCreditMapper.selectActiveCreditById(a.getActId()).getActName());
        }
        return PageInfo.of(list);
    }

    /**
     * 添加一条审核记录表
     * @param first
     */
    public void firstCord(First first) {
        first.setCreateTime(LocalDateTime.now());
        teacherMapper.firstRecord(first);
    }

    /**
     * 教师获取审核记录列表
     */
    public PageInfo<First> getRecord(First first, Integer page, Integer size) {
        PageHelper.startPage(page,size);
        List<First> list = teacherMapper.selectRecord(first);
        return PageInfo.of(list);
    }

    /**
     * 根据记录id查询记录信息
     */
    public ApplicationRecord getRecordById(First first, Integer page, Integer size) {
        ApplicationRecord record = teacherMapper.getRecordById(first.getRecordId());
        record.setStuName(studentMapper.selectStudentById(record.getStuId()).getRealname());
        record.setTypeName(activeTypeMapper.selectActiveTypeById(record.getTypeId()).getName());
        record.setActName(activeCreditMapper.selectActiveCreditById(record.getActId()).getActName());
        return record;
    }
}
